package com.cc.service.impl;

import com.cc.entity.SalChance;
import com.cc.entity.SysUser;
import com.cc.entity.SysUserExample;
import com.cc.mapper.SalChanceMapper;
import com.cc.mapper.SysUserMapper;
import com.cc.service.ISalChanceService;
import com.cc.util.SqlSessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SalChanceServiceImpl implements ISalChanceService {
    @Resource
    private SalChanceMapper salChanceMapper;

    @Override
    public PageInfo<SalChance> list(int page,int pageSize) {
        List<SalChance> salChanceList = null;
        try {
//            salChanceMapper = SessionUtil.openSession().getMapper(SalChanceMapper.class);
            PageHelper.startPage(page, pageSize);
            salChanceList = salChanceMapper.selectByExample(null);
        }finally {
            SqlSessionUtil.close();
        }
        return new PageInfo(salChanceList);
    }

    @Override
    public SalChance selectChanceRelationUser(int chanceId) {
        SalChance salChance= null;
        try{
//            salChanceMapper = SessionUtil.openSession().getMapper(SalChanceMapper.class);
            SysUserMapper userMapper = SqlSessionUtil.openSession().getMapper(SysUserMapper.class);
           ///////////////TODO 使用级联的方式/////////////////////////
            //1.根据id查询销售机会信息   //2.查询所有销售员
//            salChance = salChanceMapper.selectChanceRelationUser(chanceId);
            ///////////////TODO 使用级联的方式/////////////////////////

            ///////////////TODO 分步查询的方式/////////////////////////
            //1.根据id查询销售机会信息
            salChance = salChanceMapper.selectByPrimaryKey(chanceId);
            //2.查询所有销售员
            SysUserExample example = new SysUserExample();
            //select * from sys_user where usr_role_id=5 and usr_flag=1
            SysUserExample.Criteria criteria = example.createCriteria();
            criteria.andUsrRoleIdEqualTo(5).andUsrFlagEqualTo(1);

            List<SysUser> userList = userMapper.selectByExample(example);

            //3.将usreList赋值chance
            salChance.setUserList(userList);
            ///////////////TODO 分步查询的方式/////////////////////////
        }finally {
            SqlSessionUtil.close();
        }
        return salChance;
    }
}
